package com.gxkj.movie.controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxkj.movie.controllers.seo.IndexSeo;
import com.gxkj.movie.entitys.PlayItem;
import com.gxkj.movie.helpers.BaiduSpiderHelper;
import com.gxkj.movie.helpers.MovSiteHelpper;
import com.gxkj.movie.helpers.VodRecommendHelpper;
import com.gxkj.movie.threads.BaiduSubmitUrlThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author sqyc
 */
@Controller
@Slf4j
public class VodPlayController extends BaseController {

    @Autowired
    private MovSiteHelpper movSiteHelpper;

    @Autowired
    private VodRecommendHelpper vodRecommendHelpper;

    @Autowired
    private BaiduSpiderHelper baiduSpiderHelper;

    protected static Map<String,JSONObject> vodCache = new HashMap<>();

    @GetMapping("vodread")
    public String vodread(HttpServletRequest req, Model model,String id,@RequestParam(value = "forceclear" ,required=false,defaultValue ="false") Boolean forceclear){

        renderHeader(  req,   model);

        if(forceclear != null && forceclear){
            vodCache.clear();
        }
        renderRecommendVod(req,model,id,null);
        return getTempletate(req)+"/vod_read";
    }

    @GetMapping("vodplay")
    public String vodPlay(HttpServletRequest req, Model model,String id,Integer playIndex,@RequestParam(value = "forceclear" ,required=false,defaultValue ="false") Boolean forceclear){

        renderHeader(  req,   model);

        if(forceclear != null && forceclear){
            vodCache.clear();
        }
        renderRecommendVod(req,model,id,playIndex);
        return getTempletate(req)+"/vod_play";
    }
    @Override
    public void renderHeader(HttpServletRequest req, Model model){
        super.renderHeader(req,model);
        model.addAttribute("menu", MenuEnum.INDEX.getKey());
        model.addAttribute("TITLE", IndexSeo.TITLE);
        model.addAttribute("KEYWORDS",IndexSeo.KEYWORDS);
        model.addAttribute("DESCRIPTION",IndexSeo.DESCRIPTION);
        model.addAttribute("SITE_NAME", IndexSeo.SITE_NAME);
    }

    public void  renderRecommendVod(HttpServletRequest req, Model model,String id,Integer playIndex){

        String szDate = "";
        if(!StringUtils.isEmpty(id)){
            JSONObject recomentMov = null;
            if(vodCache.get(szDate+"-"+id) != null){
                recomentMov = vodCache.get(szDate+"-"+id);
            }else{
                recomentMov = vodRecommendHelpper.findRecommendVodById(id);
                log.info( JSON.toJSONString(recomentMov));
                if(recomentMov != null){
                    vodCache.put(szDate+"-"+id,recomentMov);
                }

            }
            List<PlayItem> playList = new ArrayList<>();
            PlayItem playItem = null;
            if(!StringUtils.isEmpty(recomentMov.getString("vodPlayUrl"))){
                    String vodPlayUrl = recomentMov.getString("vodPlayUrl");
                    String[] vodPlayUrls = vodPlayUrl.split("\r\n");
                    if(vodPlayUrls.length== 1){
                        String row = vodPlayUrls[0];
                        String[] infos =row.split("\\$");
                        PlayItem item = new PlayItem();
                        if(infos.length == 1){
                            item.setShowTitle(recomentMov.getString("vodName"));
                            item.setVodPlayUrl(infos[0]);
                            playList.add(item);
                        }else {
                            item.setShowTitle(infos[0]);
                            item.setVodPlayUrl(infos[1]);
                            playList.add(item);
                        }
                        playItem = item;
                    }else {
                        String row = null;
                        for (int index = 0;index < vodPlayUrls.length;index ++){
                            row = vodPlayUrls[index];
                            String[] infos =row.split("\\$");
                            PlayItem item = new PlayItem();
                            if(infos.length == 1){
                                item.setShowTitle(recomentMov.getString("vodName"));
                                item.setVodPlayUrl(infos[0]);
                                playList.add(item);
                            }else {
                                item.setShowTitle(infos[0]);
                                item.setVodPlayUrl(infos[1]);
                                playList.add(item);
                            }
                            if(playIndex != null && playIndex == index){
                                playItem = item;
                            }
                        }
                    }
            }

            model.addAttribute("recomentMov",recomentMov);
            model.addAttribute("playList",playList);
            model.addAttribute("playItem",playItem);
            model.addAttribute("KEYWORDS",IndexSeo.KEYWORDS+(recomentMov==null?"":("-"+recomentMov.getString("vodName"))));
            model.addAttribute("TITLE", IndexSeo.TITLE+(recomentMov==null?"":("-"+recomentMov.getString("vodName"))));
        }
    }
}
